package com.yungu.swift.api.route.v1.driver;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.driver.model.dto.DriverCheckDto;
import com.yungu.swift.assets.driver.service.CarService;
import com.yungu.swift.assets.driver.service.DriverCheckService;
import com.yungu.swift.assets.driver.service.DriverService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.constants.DriverConstant;
import com.yungu.swift.system.marketing.model.dto.MarketingInviteLogDto;
import com.yungu.swift.system.marketing.service.MarketingInviteLogService;
import com.yungu.swift.system.sys.service.SysCompanyService;
import com.yungu.swift.system.sys.service.SysRegionalAgentService;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cuixiuyin
 * @date 2018/1/24
 */
@Controller
@RequestMapping(value = "/admin/taxiDriver")
@Api(value = "【出租车司机】【注册】", tags = "司机注册相关接口")
public class TaxiDriRegisterApi {

    @Reference
    private DriverCheckService driverCheckService;
    @Reference
    private MarketingInviteLogService marketingInviteLogService;
    @Reference
    private CarService carService;
    @Reference
    private DriverService driverService;

    @RequestMapping(value = "/addRegister", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "注册接口", response = ResponseData.class)
    public ResponseData<Object> addRegister(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam(value = "appid", required = true) String appid,
            @ApiParam(name = "mobile", value = "司机手机号", required = true) @RequestParam(value = "mobile", required = true) String mobile,
            @ApiParam(name = "face", value = "头像", required = true) @RequestParam(value = "face", required = true) String face,
            @ApiParam(name = "name", value = "姓名", required = true) @RequestParam(value = "name", required = true) String name,
            @ApiParam(name = "idCard", value = "身份证号", required = true) @RequestParam(value = "idCard", required = true) String idCard,
            @ApiParam(name = "idCardFaceImg", value = "身份证正面照", required = true) @RequestParam(value = "idCardFaceImg", required = true) String idCardFaceImg,
            @ApiParam(name = "idCardBackImg", value = "身份证背面照", required = true) @RequestParam(value = "idCardBackImg", required = true) String idCardBackImg,
            @ApiParam(name = "getDriverLicenseDate", value = "初次领取驾照日期", required = true) @RequestParam(value = "getDriverLicenseDate", required = true) @DateTimeFormat(pattern = "yyyy-MM-dd") Date getDriverLicenseDate,
            @ApiParam(name = "drivingLicenceHomeImg", value = "驾驶证正面照片", required = true) @RequestParam(value = "drivingLicenceHomeImg", required = true) String drivingLicenceHomeImg,
            @ApiParam(name = "drivingLicenceSubImg", value = "驾驶证反面照片", required = true) @RequestParam(value = "drivingLicenceSubImg", required = true) String drivingLicenceSubImg,
            @ApiParam(name = "certificateNo", value = "从业资格证号", required = true) @RequestParam(value = "certificateNo", required = true) String certificateNo,
            @ApiParam(name = "networkCarProofOff", value = "从业资格有效期止", required = true) @RequestParam(value = "networkCarProofOff", required = true) @DateTimeFormat(pattern = "yyyy-MM-dd") Date networkCarProofOff,
            @ApiParam(name = "certificateImage", value = "从业资格证照片", required = true) @RequestParam(value = "certificateImage", required = true) String certificateImage,
            @ApiParam(name = "agentUuid", value = "代理uuid", required = true) @RequestParam(value = "agentUuid", required = true) String agentUuid,
            @ApiParam(name = "companyUuid", value = "公司uuid", required = true) @RequestParam(value = "companyUuid", required = true) String companyUuid,
            @ApiParam(name = "plateNum", value = "车牌号", required = true) @RequestParam(value = "plateNum", required = true) String plateNum,
            @ApiParam(name = "licenseNo", value = "营运证号", required = true) @RequestParam(value = "licenseNo", required = true) String licenseNo,
            @ApiParam(name = "drivingPic", value = "行驶证照片(正面)", required = true) @RequestParam(value = "drivingPic", required = true) String drivingPic,
            @ApiParam(name = "drivingPicBack", value = "行驶证照片(反面)", required = true) @RequestParam(value = "drivingPicBack", required = true) String drivingPicBack,
            @ApiParam(name = "operationCertificateImg", value = "道路运输证照片", required = true) @RequestParam(value = "operationCertificateImg", required = true) String operationCertificateImg,
            @ApiParam(name = "monitoringCardImg", value = "监督卡照片", required = true) @RequestParam(value = "monitoringCardImg", required = true) String monitoringCardImg,
            @ApiParam(name = "mancarImg", value = "人车合影", required = true) @RequestParam(value = "mancarImg", required = true) String mancarImg,
            @ApiParam(name = "inviter", value = "邀请人手机号") @RequestParam(value = "inviter", required = false) String inviter,
            @ApiParam(name = "inviterType", value = "邀请人类型（1乘客,2司机）") @RequestParam(value = "inviterType", required = false) Integer inviterType
    ) {
        Map<String, Object> map = new HashMap<>(16);
        map.put("idCard", idCard);
        map.put("appid", appid);
        if (driverService.getValidDriver(map).isSuccess()) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "身份证号已存在，请联系管理员");
        }
        map.clear();
        map.put("mobile", mobile);
        map.put("appid", appid);
        map.put("type", CommonConstant.BUSINESS_TYPE_TAXI);

        DriverCheckDto checkDto = driverCheckService.getLatest(map).getData();
        //待审核或者已通过
        if (checkDto != null && !checkDto.getStatus().equals(DriverConstant.DRIVER_CHECK_NOT_PASS)) {
            return ResponseData.buildSuccessResponse("已经注册了", null);
        }
        DriverCheckDto driverCheckDto = new DriverCheckDto();
        driverCheckDto.setAppid(appid);
        driverCheckDto.setMobile(mobile);
        driverCheckDto.setFace(face);
        driverCheckDto.setName(name);
        driverCheckDto.setIdCard(idCard);
        driverCheckDto.setLicenseNum(idCard);
        driverCheckDto.setIdCardFaceImg(idCardFaceImg);
        driverCheckDto.setIdCardBackImg(idCardBackImg);
        driverCheckDto.setGetDriverLicenseDate(getDriverLicenseDate);
        driverCheckDto.setDrivingLicenceHomeImg(drivingLicenceHomeImg);
        driverCheckDto.setDrivingLicenceSubImg(drivingLicenceSubImg);
        driverCheckDto.setCertificateNo(certificateNo);
        driverCheckDto.setNetworkCarProofOff(networkCarProofOff);
        driverCheckDto.setCertificateImage(certificateImage);
        driverCheckDto.setAgentUuid(agentUuid);
        driverCheckDto.setCompanyUuid(companyUuid);
        driverCheckDto.setPlateNum(plateNum);
        driverCheckDto.setLicenseNo(licenseNo);
        driverCheckDto.setDrivingPic(drivingPic);
        driverCheckDto.setDrivingPicBack(drivingPicBack);
        driverCheckDto.setOperationCertificateImg(operationCertificateImg);
        driverCheckDto.setMonitoringCardImg(monitoringCardImg);
        driverCheckDto.setMancarImg(mancarImg);
        driverCheckDto.setCreateOn(new Date());
        driverCheckDto.setUuid(StringUtils.buildUUID());
        driverCheckDto.setBusinessType(CommonConstant.BUSINESS_TYPE_TAXI);
        driverCheckDto.setStatus(DriverConstant.DRIVER_CHECK_PEND);
        driverCheckService.add(driverCheckDto);
        //如果有邀请人关系 则记录邀请信息
        if (StringUtils.isNotEmpty(inviter)) {
            //记录邀请关系
            MarketingInviteLogDto inviteLogDto = new MarketingInviteLogDto();
            inviteLogDto.setUuid(StringUtils.buildUUID());
            inviteLogDto.setInvitee(mobile);
            inviteLogDto.setInviter(inviter);
            inviteLogDto.setInviterType(inviterType);
            inviteLogDto.setInviteeType(2);
            inviteLogDto.setCreateOn(new Date());
            marketingInviteLogService.add(inviteLogDto);
        }
        return ResponseData.buildSuccessResponse("注册成功", null);
    }

    @RequestMapping(value = "/checkCar", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "校验车牌号是否存在", response = ResponseData.class)
    public ResponseData<Object> checkCar(
            @ApiParam(name = "plateNum", value = "车牌号", required = true) @RequestParam(value = "plateNum", required = true) String plateNum) {
        Map<String, Object> params = new HashMap<>(3);
        params.put("plateNum", plateNum);
        if (carService.get(params) != null) {
            return ResponseData.buildSuccessResponse("校验成功", null);
        }

        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "校验失败");

    }


}
